<template>
  <div>

  <el-dialog :title='isAdd?"角色添加":"角色编辑"' :visible.sync="isShow" :before-close="cancel"  >
  <el-form :model="roleFrom"  :rules="rules" ref='roleFrom'>
    <el-form-item label="角色名称" :label-width="formLabelWidth" prop="rolename">
      <el-input  autocomplete="off" v-model="roleFrom.rolename"></el-input>
    </el-form-item> -
   <el-form-item label="角色权限" :label-width="formLabelWidth">
     <el-tree
     check-strictly
      ref="tree"
  :data="getMenuData"
  show-checkbox
  node-key="id"
 default-expand-all
  :props="{
    children:'children',
    label:'title'
  }">
</el-tree>
    </el-form-item>
     <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="roleFrom.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button>取 消</el-button>
    <el-button type="primary" @click="confirm">确 定</el-button>
  </div>
</el-dialog>
  </div>
</template>

<script>
import { mapGetters, mapActions } from 'vuex'
import { roleadd, roleedit, roleinfo } from '../../../api/api.js'
export default {
  props: ['isShow', 'isAdd'],
  data () {
    return {
      formLabelWidth: '120px',
      roleFrom: {
        rolename: '', // 角色名称
        menus: '', // 角色权限 数组id
        status: 1 // 状态1正常2禁用
      },
      rules: {
        rolename: [
          { required: true, message: '请输入菜单名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]

      }
    }
  },
  computed: {
    ...mapGetters('menu', ['getMenuData'])

  },
  mounted () {
    // 放置 页面刷新数据丢失
    this.getRolelist()

    if (this.getMenuData.length === 0) {
      this.$store.dispatch('menu/getMeulist')
    }
  },
  methods: {
    ...mapActions('role', ['getRolelist']),
    cancel () {
      this.$emit('cancel', false)
      this.reset()
    },
    confirm () {
      this.$refs.roleFrom.validate(val => {
        if (val) {
          // 需要的数据是 字符串  获得的是arr
          this.roleFrom.menus = this.$refs.tree.getCheckedKeys().join(',')
          if (this.isAdd) {
            roleadd(this.roleFrom).then(res => {
              if (res.code === 200) {
                this.$message.success(res.msg)
                this.getRolelist()
                this.cancel()
              } else {
                this.$message.error(res.msg)
              }
            })
          } else {
            roleedit(this.roleFrom).then(res => {
              if (res.code === 200) {
                this.$message.success(res.msg)
                // 关闭弹框
                this.cancel()
                // 获取列表
                this.getRolelist()
              } else {
                this.$message.error(res.msg)
              }
            })
          }
        }
      })
    },
    reset () {
      this.roleFrom = {
        rolename: '', // 角色名称string
        menus: '', // 角色权限 id数组'2,3,4' 字符串
        status: 1 // 状态1正常2禁用number
      }
      this.$refs.roleFrom.resetFields()
      // 清空树节点的选中状态
      this.$refs.tree.setCheckedKeys([])
    },
    async lookup (id) {
      const res = await roleinfo({ id })
      this.roleFrom = res.list
      this.roleFrom.id = id
      // 获取目前勾选的节点 接收两个参数，1. 勾选节点的 key 的数组
      // 将字符传转为数组
      this.$refs.tree.setCheckedKeys(this.roleFrom.menus ? this.roleFrom.menus.split(',') : [])
    }

  }
}
</script>

<style>

</style>
